﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Maticsoft.DBUtility;

public partial class Page_Main : System.Web.UI.Page
{
    PagedDataSource PDS = new PagedDataSource(); //PagedDataSource 实现分页  
    protected void Page_Load(object sender, EventArgs e)
    {
        InserHomePageBrowser();
        System.Web.UI.HtmlControls.HtmlInputButton ib = Page.Master.FindControl("ctl00$searchBtn") as System.Web.UI.HtmlControls.HtmlInputButton;
        ib.ServerClick += new EventHandler(ib_ServerClick);
        if (!IsPostBack)
        {
            BindRepeater(null);
        }
    }
    private void InserHomePageBrowser()
    {
        string sql = "select browserCount from TB_HomePageBrowser where ID=1";
        object obj = DbHelperOleDb.GetSingle(sql);
        int browserCount=0;
        if (obj == null)
        {
            return;
        }
        browserCount = Convert.ToInt32(obj);
        browserCount++;
        sql = "update TB_HomePageBrowser set browserCount="+browserCount+" where  ID=1";
        DbHelperOleDb.ExecuteSql(sql);
        lblBrowser.Text = browserCount.ToString(); ;
    }
    void ib_ServerClick(object sender, EventArgs e)
    {
        System.Web.UI.HtmlControls.HtmlInputControl txtKey = Page.Master.FindControl("ctl00$key") as System.Web.UI.HtmlControls.HtmlInputControl;
        BindRepeater(" and address like '%" + txtKey.Value + "%'");
    }
    private void BindRepeater(string where)
    {
        int totalCount = 0;//总记录条数  
        int totalPage = 1;//总页数  
        string normalWhere = " where 1=1  ";
        string sqlWhere = normalWhere + SearchByArea() + SearchByProportionID() + where;
        string sql = "select * from TB_House" + sqlWhere;
        DataTable table = DbHelperOleDb.Fill(sql);
        totalCount = table.DefaultView.Count;
        PDS.DataSource = table.DefaultView; //设置pagedatasource 的属性        
        PDS.AllowPaging = true; //是否允许分页  
        PDS.PageSize = 10;//每页显示记录数量  
        int curPage;
        if (Request.QueryString["page"] != null)
            curPage = Convert.ToInt32(Request.QueryString["page"]);// 为四个链接准备  
        else
            curPage = 1;
        if (totalCount == 0)//如果没有记录，就一页  
            totalPage = 1;
        else
        {
            if (totalCount % PDS.PageSize == 0) //如果是整数页  
                totalPage = totalCount / PDS.PageSize;
            else
                totalPage = totalCount / PDS.PageSize + 1; //如果非整数页  
        }
        PDS.CurrentPageIndex = curPage - 1;
        lblCurrentPage.Text = "共" + totalCount.ToString() + "条记录，当前页" + curPage.ToString() + "/" + totalPage;
        lnkFrist.NavigateUrl = Request.CurrentExecutionFilePath + "?page=1";//获取当前请求的虚拟路径  
        if (!PDS.IsFirstPage) //如果不是第一页  
            lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToInt32(curPage - 1);
        if (!PDS.IsLastPage) //如果不是最后一页  
            lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToInt32(curPage + 1);
        lnkEnd.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + totalPage;
        repeaterCompany.DataSource = PDS;
        repeaterCompany.DataBind();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string key = txtKey.Text;
        BindRepeater(" and address like '%" + key + "%'");
    }
    private string SearchByArea()
    {
        string areaID = Request["areaID"];
        string sqlWhere = string.Empty;
        if (areaID != null && areaID != "0")
        {
            sqlWhere = " and areaID=" + AddQuotes(areaID.ToString());
        }
        return sqlWhere;
    }
    private string SearchByProportionID()
    {
        string proportionID = Request["proportionID"];
        if (string.IsNullOrEmpty(proportionID))
        {
            return null;
        }
        int minProportion = 0, maxProportion = 0;
        string sql = "select minProportion,maxProportion from TB_Proportion where ID=" + proportionID;
        DataTable table = DbHelperOleDb.Fill(sql);
        if (table != null && table.Rows.Count > 0)
        {
            minProportion = Convert.ToInt32(table.Rows[0]["minProportion"]);
            maxProportion = Convert.ToInt32(table.Rows[0]["maxProportion"]);
        }
        string sqlWhere = " and proportionSum between " + minProportion + " and " + maxProportion + "";
        return sqlWhere;
    }
    protected string getAreaName(int areaID)
    {
        string sql = "select areaName from TB_Area where ID=" + areaID;
        return DbHelperOleDb.GetSingle(sql).ToString();
    }
    private string AddQuotes(string str)
    {
        if (string.IsNullOrEmpty(str))
        {
            return null;
        }
        return "'" + str + "'";
    }
}

